package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.NotifyGroupUser;
import org.kl.bf.repository.basic.NotifyGroupUserDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class NotifyGroupUserService {
	
	private NotifyGroupUserDao notifyGroupUserDao;
	public NotifyGroupUser getNotifyGroupUser(String guid) {
		return notifyGroupUserDao.findOne(guid);
	}
	
	public List<NotifyGroupUser> getNotifyGroupUser(){
		return notifyGroupUserDao.findAll();
	}

	public void saveNotifyGroupUser(NotifyGroupUser entity) {
		notifyGroupUserDao.save(entity);
	}

	public void deleteNotifyGroupUser(String guid) {
		notifyGroupUserDao.delete(guid);
	}
	
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				notifyGroupUserDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	public List<NotifyGroupUser> getAllNotifyGroupUser() {
		return notifyGroupUserDao.findAll();
	}

	public Page<NotifyGroupUser> getNotifyGroupUser(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<NotifyGroupUser> spec = buildSpecification(searchParams);

		return notifyGroupUserDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<NotifyGroupUser> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<NotifyGroupUser> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), NotifyGroupUser.class);
		return spec;
	}

	@Autowired
	public void setNotifyGroupUserDao(NotifyGroupUserDao notifyGroupUserDao) {
		this.notifyGroupUserDao = notifyGroupUserDao;
	}

}
